﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Security.Cryptography;

namespace IronSharePoint
{
    public class IronSettings : SPPersistedObject
    {
        [Persisted]
        private List<string> _trustedLocations;

        [Persisted]
        private byte[] _privateKey = null;


        public IronSettings ()
        {
            _trustedLocations = new List<string>();
        }

        public IronSettings(string strName, SPPersistedObject objParent, Guid objGuid)
        : base(strName, objParent, objGuid) 
        {
            _trustedLocations = new List<string>();
        }

        public List<string> TrustedLocations
        {
            get { return _trustedLocations; }
        }

        public byte[] PrivateKey
        {
            get { return _privateKey; }
            set { _privateKey = value; }
        }

        public static IronSettings Current
        {
            get
            {
                return SPFarm.Local.GetObject(IronConstant.StoreId) as IronSettings;
            }
        }

        public void CreateNewPrivateKey()
        {
            CspParameters parameters = new CspParameters();
            parameters.Flags = CspProviderFlags.UseMachineKeyStore;

            RSACryptoServiceProvider rsaAlg = new RSACryptoServiceProvider(parameters);
            rsaAlg.PersistKeyInCsp = false;
            PrivateKey = rsaAlg.ExportCspBlob(true);
        }

    }
}
